#include <stdio.h>
#include <vector>
#include <map>
#include <algorithm>
#include <list>
#include <numeric>

using namespace std;


vector<int> alphabet;
map<int,int> spectr;

int sum(vector<int>& vec, int start, int count)
{
	int sum = 0;
	for (int i=start; i<start+count; i++)
	{
		sum += vec[i];
	}
	return sum;
}

map<int,int> getCyclopeptide(list<int> input)
{
	int size = input.size();
	map<int,int> peptide;
	list<int> copy(input);
	copy.reverse();
	vector<int> dbl_str;
	dbl_str.insert(dbl_str.begin(), copy.begin(), copy.end());
	dbl_str.insert(dbl_str.begin(), copy.begin(), copy.end());
	for (int i=1; i<size; i++)
	{
		for (int j=0; j<size; j++)
		{
			int current = sum(dbl_str, j, i);
            auto f = peptide.find(current);
            if (f == peptide.end())
                peptide.insert(pair<int,int>(current, 1));
            else
                f->second = f->second + 1;
		}
	}
    peptide.insert(pair<int,int>(0,1));
	peptide.insert(pair<int,int>(sum(dbl_str, 0, copy.size()),1));
	return peptide;
}

int score(map<int,int> peptide)
{
    int score = 0;
    int p;
    for (auto it=spectr.begin(); it!=spectr.end(); it++)
    {
        if (it->first == 0)
            continue;
        auto f = peptide.find(it->first);
        if (f != peptide.end())
        {
            if (it->second > f->second)
            {
                score += f->second;
            } else
                score += it->second;
        }
    }
    return score;
}

void expand(list<pair<list<int>, int>>* from, list<pair<list<int>, int>>* to)
{
	for (auto it=from->begin(); it!=from->end(); it++)
	{
		
		for (auto ch=alphabet.begin(); ch!=alphabet.end(); ch++)
		{
            list<int> cur = it->first;
			cur.push_back(*ch);
			to->push_back(pair<list<int>,int>(cur, score(getCyclopeptide(cur))));
		}
	}
}

int mass(map<int,int> peptide)
{
    return peptide.rbegin()->first;
}

bool score_compare(pair<list<int>, int> a, pair<list<int>, int> b)
{
    return a.second > b.second;
}

void cut(list<pair<list<int>, int>>* from, list<pair<list<int>, int>>* to, int N)
{
    if (from->size() > N)
	{
        //from->sort(score_compare);
		stable_sort(from->begin(), from->end(), score_compare);
	}
    auto end = from->begin();
    for (int i=0; i<min(N, (int) from->size()); i++)
        end++;
    to->assign(from->begin(), end);
}

bool compare(pair<int,int> a, pair<int,int> b)
{
	if (a.second > b.second)
		return true;
	else if (a.second < b.second)
		return false;
	else if (a.first < b.first)
		return false;
	else
		return true;
}

int main()
{
	FILE* file = fopen("spectrum_25_tyrocidine_b1.txt", "r");
	FILE* out = fopen("result.txt", "w");
	int M,N;
	fscanf(file, "%d %d", &M, &N);
	vector<int> sp;
	while (!feof(file))
	{
		int num;
		fscanf(file, "%d ", &num);
		sp.push_back(num);
		auto f = spectr.find(num);
		if (f == spectr.end())
			spectr.insert(pair<int,int>(num, 1));
		else
			f->second = f->second + 1;
	}
	int spectr_mass = mass(spectr);

	map<int,int> diff;
	for (int i=0; i<sp.size(); i++)
	{
		for (int j=0; j<sp.size(); j++)
		{
			int dif = sp[i] - sp[j];
			if (dif > 0) {
				auto f = diff.find(dif);
				if (f == diff.end())
					diff.insert(pair<int,int>(dif, 1));
				else
					f->second = f->second + 1;
			}
		}
	}

	vector<pair<int,int>> vec(diff.begin(), diff.end());

	sort(vec.begin(), vec.end(), compare);
	int count = 0;
	for (auto it=vec.begin(); it!=vec.end(); it++)
	{
		if (count == M)
			break;
		if ((it->first >= 57) && (it->first <= 200))
		{
			alphabet.push_back(it->first);
			count++;
		}
	}

	// start
	int myints[] = {137,57,129,99,71};
	list<int> tep(myints, myints+5);
	int mas = mass(getCyclopeptide(tep));
	int sc = score(getCyclopeptide(tep));

	list<pair<list<int>, int>>* cur = new list<pair<list<int>, int>>();
	list<pair<list<int>, int>>* prev = new list<pair<list<int>, int>>();
    
    prev->push_back(pair<list<int>, int>(list<int>(),0));
    int leader_score = 0;
    list<list<int>> leaders;

    while (!prev->empty())
    {
        expand(prev, cur);
        swap(cur, prev);
        cur->clear();
        for (auto it=prev->begin(); it!=prev->end(); it++)
        {
            map<int,int> peptide = getCyclopeptide(it->first);
            int peptide_mass = mass(peptide);
            
            if (peptide_mass > spectr_mass) 
                continue;
            if (peptide_mass == spectr_mass)
            {
                if (it->second == leader_score)
                {
                    leaders.push_back(it->first);
                }
                if (it->second > leader_score)
                {
                    leader_score = score(peptide);
                    leaders.clear();
                    leaders.push_back(it->first);
                }
            }
            cur->push_back(*it);
        }
        prev->clear();
        cut(cur, prev, N);
        cur->clear();
    }

	for (auto it=leaders.begin(); it!=leaders.end(); it++)
	{
		int size = it->size();
		for(int i=0; i<size-1; i++)
		{
			fprintf(out, "%d-", it->front());
			it->pop_front();
		}
		fprintf(out, "%d ", it->front());
	}

	return 0;
}